const express = require('express')
const { querySwiper, queryHomeByHomeName, queryPromo, queryBanner } = require('../mysql/home')
const { queryGoodsPriceById, queryGoodsDiscountByIdAndPrice } = require('../mysql/goodsInfo')
const { queryGoodsByCategotyTwoId, queryGoodsByCategoryOneId } = require('../mysql/goods')
const { queryCategoryOne } = require('../mysql/category')
const Result = require('../result/result')
const { NO_PERMISSION } = require('../result/reponseState')

// swiper
const router = express.Router()

router.get('', async (req, res) => {
  try {
    const resultArr = []
    const promiseArr = []
    const result = await queryHomeByHomeName('手机')
    const pro = (e, eindex) => {
      return new Promise(async (resolve, reject) => {
        const price = await queryGoodsPriceById(e.goods_id)
        let count = price.length > 1 ? true : false
        let index = 0
        for (let i = 0; i < price.length; i++) {
          if (price[i].goods_price) {
            index = i
            break
          }
        }
        const discountArr = await queryGoodsDiscountByIdAndPrice(e.goods_id, price[index].goods_price)
        let discount = discountArr[0].goods_discount ? discountArr[0].goods_discount : null
        let imgUrl = e.home_imgUrl ? e.home_imgUrl : e.goods_image_path
        let obj
        if (e.type === 'large') {
          obj = {
            type: e.type,
            msg: {
              goods_id: e.goods_id,
              imgUrl
            }
          }
        } else {
          obj = {
            type: e.type,
            msg: {
              goods_id: e.goods_id,
              title: e.title,
              desc: e.goods_desc,
              imgUrl,
              price: price[index].goods_price,
              count,
              discount
            }
          }
        }
        resultArr[eindex] = obj
        resolve()
      })
    }
    result.forEach((e, eindex) => {
      promiseArr.push(pro(e, eindex))
    })
    Promise.all(promiseArr).then(() => {
      res.send(Result.ok(resultArr))
    })
  } catch (error) {
    res.send(Result.error2(NO_PERMISSION, error))
  }
})

router.get('/promo', (req, res) => {
  queryPromo().then((response) => {
    res.send(Result.ok(response))
  }, (err) => {
    res.send(Result.error2(NO_PERMISSION, err))
  })
})

router.get('/banner', (req, res) => {
  queryBanner().then((response) => {
    res.send(Result.ok(response))
  }, (err) => {
    res.send(Result.error2(NO_PERMISSION, err))
  })
})

router.get('/show', async (req, res) => {
  try {
    const resultArr = []
    const promiseArr = []
    const result = await queryGoodsByCategotyTwoId(req.query.categoryTwo_id)
    const pro = (e, eindex) => {
      return new Promise(async (resolve, reject) => {
        const price = await queryGoodsPriceById(e.goods_id)
        let count = price.length > 1 ? true : false
        let index = 0
        for (let i = 0; i < price.length; i++) {
          if (price[i].goods_price) {
            index = i
            break
          }
        }
        const obj = {
          type: 'show',
          msg: {
            goods_id: e.goods_id,
            title: e.goods_name,
            price: price[index].goods_price,
            count,
            imgUrl: e.goods_image_path
          }
        }
        resultArr[eindex] = obj
        resolve()
      })
    }
    result.forEach((e, eindex) => {
      promiseArr.push(pro(e, eindex))
    })
    Promise.all(promiseArr).then(() => {
      res.send(Result.ok(resultArr))
    })
  } catch (error) {
    res.send(Result.error2(NO_PERMISSION, error))
  }

})

router.get('/show2', async (req, res) => {
  try {
    const result = await queryGoodsByCategoryOneId(req.query.categoryOne_id)
    res.send(Result.ok(result))
  } catch (error) {
    res.send(Result.error2(NO_PERMISSION, error))
  }
})

router.get('/swiper', (req, res) => {
  querySwiper().then((response) => {
    res.send(Result.ok(response))
  }, (err) => {
    res.send(Result.error2(NO_PERMISSION, err))
  })
})

router.get('/menu', (req, res) => {
  queryCategoryOne().then((response) => {
    res.send(Result.ok(response))
  }, (err) => {
    res.send(Result.error2(NO_PERMISSION, err))
  })
})

module.exports = router